// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from "vue";
import App from "./App";
import router from "./router";
import ElementUI from "element-ui";
import "@/style/element.scss";
// import 'element-ui/lib/theme-chalk/index.css'
import store from "./store";

import axios from "./utils/axios";
Vue.prototype.$axios = axios;

Vue.config.productionTip = false;

Vue.use(ElementUI);

/*访问每个路由前调用钩子函数*/
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth === "user") {
    //先判断是否需要登录
    if (store.state.user && store.state.user.userType === "NORMAL") {
      //需要则判断store里是否有user的信息
      next(); /*已登录，则正常跳转至用户界面*/
    } else {
      next({
        path: "login",
        query: { redirect: to.fullPath }
      });
    }
  } else if (to.meta.requireAuth === "admin") {
    if (store.state.user && store.state.user.userType === "ADMIN") {
      //需要则判断store里是否有user的信息
      next(); /*已登录，则正常跳转至用户界面*/
    } else {
      next({
        path: "login",
        query: { redirect: to.fullPath }
      });
    }
  } else {
    next();
  }
});

/* eslint-disable no-new */
new Vue({
  el: "#app",
  render: h => h(App),
  router,
  store,
  components: { App },
  template: "<App/>"
});
